import ccclass = cc._decorator.ccclass;
import Drawable from "../Domain/Drawable";
import Utils from "../utils/Utils";

@ccclass
export default class LineContainer extends cc.Component {

    private graphic: cc.Graphics = null

    private drawable: Drawable = null
    private points: cc.Vec2[] = null

    private drawSpeed: number = 2
    private drawFlag: boolean = false
    private currentLen: number = 0
    private currentMatchIndex: number = 0

    onLoad() {
        if (!this.graphic) {
            this.graphic = this.addComponent(cc.Graphics)
        } else {
            this.graphic = this.getComponent(cc.Graphics)
        }
        this.node.setAnchorPoint(0, 0)
    }

    start() {

    }

    init(drawable: Drawable) {
        this.drawable = drawable
        this.setLinePoints(this.drawable.path)
        this.graphic.clear(true)
        this.node.width = this.drawable.bound[0]
        this.node.height = this.drawable.bound[1]
        this.currentLen = 0
        this.currentMatchIndex = 0
    }

    setLinePoints(points: cc.Vec2[]) {
        this.points = points || [cc.v2()]
        if (this.points.length <= 0) {
            this.points.push(cc.v2())
        }
    }

    startDraw() {
        this.drawFlag = true
        this.currentLen = 0
    }

    endDraw() {
        this.drawFlag = false
        this.startMatch()
    }

    startMatch() {
        this.currentMatchIndex = 1
        this.schedule(function () {
            this.matchLoop()
        }.bind(this), 0.5, this.points.length + 1)
    }

    private matchLoop() {
        this.graphic.clear(false)
        let gameOver: boolean = false
        do {
            // if (this.currentMatchIndex >= this.points.length) {
            //     console.error("check End 0.")
            //     gameOver = true
            //     return
            // }

            let p0 = this.points[0]
            this.graphic.moveTo(p0.x, p0.y)
            let p1 = null
            let totalLen: number = this.currentLen

            for (let i = 1; i <= this.currentMatchIndex; i++) {
                if (i == this.points.length) i = 0
                p1 = this.points[i]
                totalLen -= cc.pDistance(p1, p0)
                if (totalLen <= 0) {
                    console.error("线长度不够,游戏结束:")
                    gameOver = true
                    break
                }
                this.graphic.lineTo(p1.x, p1.y)
                p0 = p1
            }
            if (gameOver) break

            if (this.currentMatchIndex == this.points.length) {
                this.currentMatchIndex = 0
                gameOver = true
            }
            p1 = this.points[this.currentMatchIndex]

            this.graphic.stroke()

            this.currentMatchIndex++;
        } while (false)
        if (gameOver) {
            console.error("GameOver")
        }
    }

    private drawLine(height: number) {
        let p = this.points[0]
        this.graphic.clear(false)
        this.graphic.moveTo(p.x, p.y)
        this.graphic.lineWidth = 5
        this.graphic.strokeColor = cc.Color.BLACK
        this.graphic.lineTo(p.x, p.y + height)
        this.graphic.stroke()
    }

    update() {
        if (this.drawFlag) {
            this.drawLine(this.currentLen += this.drawSpeed)
        }
    }
}